<template>

    <div>

        <h2>页面配置</h2>

        <div class="container">

            <div style="margin-bottom: 20px;">
                <el-button type="default" @click="add">添加页面</el-button>
            </div>

            <el-tabs v-model="currentTab">
                <el-tab-pane
                        label="列表"
                        name="list"
                >

                    <el-card class="box-card" v-for="item in items" :key="item.id"
                              style="margin-top: 10px;">
                        <div slot="header" class="clearfix">
                            <span>{{item.title}}</span>
                            <el-button style="float: right; padding: 3px 0" type="text" @click="edit(item)">修改</el-button>
                        </div>
                        <div class="text">
                            <p>页面路径: {{item.name}}</p>
                        </div>
                    </el-card>

                </el-tab-pane>
                <el-tab-pane label="添加页面" name="add" v-if="currentTab == 'add'">
                    <el-form ref="form" :model="form" label-width="80px">

                        <el-form-item label="页面路径">
                            <el-input v-model="form.name"></el-input>
                        </el-form-item>

                        <el-form-item label="页面标题">
                            <el-input v-model="form.title"></el-input>
                        </el-form-item>

                        <el-form-item label="内容">
                            <editor style="width: 480px;" ref="editor1" v-model="form.content"></editor>
                        </el-form-item>

                        <el-form-item >
                            <el-button @click="goToTab('list')">取 消</el-button>
                            <el-button type="primary" @click="save">确 定</el-button>
                        </el-form-item>

                    </el-form>
                </el-tab-pane>
                <el-tab-pane label="修改页面" name="edit" v-if="currentTab == 'edit'">
                    <el-form ref="form" :model="form" label-width="80px">

                        <el-form-item label="页面路径">
                            <el-input v-model="form.name" readonly></el-input>
                        </el-form-item>

                        <el-form-item label="页面标题">
                            <el-input v-model="form.title"></el-input>
                        </el-form-item>

                        <el-form-item label="内容">
                            <editor ref="editor2" v-model="form.content"></editor>
                        </el-form-item>

                        <el-form-item >
                            <el-button @click="goToTab('list')">取 消</el-button>
                            <el-button type="primary" @click="save">确 定</el-button>
                        </el-form-item>

                    </el-form>
                </el-tab-pane>
            </el-tabs>

        </div>


    </div>

</template>
<script>
  import editor from './PageEditor.vue';
    export default {
      components: {
        editor
      },
      data() {
        return {
          currentTab: 'list',
          items: [],
          loading: true,
          params: {
            //
          },
          form: {
            is_category: 0,
            parent: null
          },
          dialogVisible: false,
        }
      },
      created() {
        this.fetch();
      },
      methods: {
        fetch() {
          axios.get('/admin/api/page', {params: this.params}).then((res) => {
            if (res.data.code === 0) {
              this.items = res.data.data.items;
            } else {
              this.$message(res.data.msg);
            }
            this.loading = false;
          }).catch((err) => {
            console.log(err);
            this.$message('系统错误');
            this.loading = false;
          })
        },

        add() {
           this.dialogVisible = true;
           this.form = {};
           this.currentTab = 'add';
        },

        save() {
          if (this.form.id !== undefined) {
            this.update();
          } else {
            this.store();
          }
        },

        store() {
          axios.post('/admin/api/page', this.form).then((res) => {
            if (res.data.code === 0) {
              this.dialogVisible = false;
              this.fetch();
              this.currentTab = 'list';
              this.$message('保存成功');
            } else {
              this.$message(res.data.msg);
            }
            this.loading = false;
          }).catch((err) => {
            this.$message('系统错误');
            this.loading = false;
          })
        },

        update() {
          axios.put('/admin/api/page/' + this.form.id, this.form).then((res) => {
            if (res.data.code === 0) {
              this.dialogVisible = false;
              this.$message('保存成功');
            } else {
              this.$message(res.data.msg);
            }
            this.loading = false;
          }).catch((err) => {
            this.$message('系统错误');
            this.loading = false;
          })
        },

        edit(item) {
          this.dialogVisible = true;
          this.form = Object.assign({}, item);
          this.currentTab = 'edit';
        }

      }
    }
</script>